package cn.itcast.bos.web.action.base;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.utils.FileUtils;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.crm.service.Customer;
import cn.itcast.crm.service.CustomerService;
@Controller
@Scope("prototype")
@Namespace("/")//<package namespace extends
@ParentPackage("struts-default")
@Results({@Result(name="list",location="/pages/base/customer.jsp")})
public class customerAction extends BaseAction<Customer> {
    @Autowired
	private CustomerService customerService;
    
    
    @Action("customerAction_exportXls")
    public String exportXls() throws Exception {
    	 List<Customer> list = customerService.findAll();
     	//创建空白POI excel文件对象
  		//XSSFWorkbook; 07
  		HSSFWorkbook workbook = new HSSFWorkbook();
  		//创建标签页
  		HSSFSheet sheet = workbook.createSheet("分区数据");
  		//创建行  1、创建标题行   2、创建数据行
  		HSSFRow headRow = sheet.createRow(0);
  		//创建单元格--写入单元格数据
  		headRow.createCell(0).setCellValue("用户名");
  		headRow.createCell(1).setCellValue("类型");
  		headRow.createCell(2).setCellValue("生日");
  		headRow.createCell(3).setCellValue("性别");
  		headRow.createCell(4).setCellValue("公司");
  		headRow.createCell(5).setCellValue("地址");
  		headRow.createCell(6).setCellValue("邮箱");
  		HSSFRow dataRow;
  		for (Customer customer : list) {
  			dataRow=sheet.createRow(sheet.getLastRowNum()+1);//sheet.getLastRowNum()获取当前sheet的被写上内容的最后一行，若想再空白一行添加数据则加1                                      
  			Cell cell0=dataRow.createCell(0);
 			Cell cell1=dataRow.createCell(1);
 			Cell cell2=dataRow.createCell(2);
 			Cell cell3=dataRow.createCell(3);
 			Cell cell4=dataRow.createCell(4);
 			Cell cell5=dataRow.createCell(5);
 			Cell cell6=dataRow.createCell(6);
 			if(customer.getUsername()!=null){
                 
                 cell0.setCellValue(customer.getUsername());
             }else{
                 cell0.setCellValue("");
             }
 			if(customer.getType()!=null){
 				 
 				cell1.setCellValue(customer.getType());
 			}else{
 				cell1.setCellValue("");
 			}
 			if(customer.getSex()!=null){
 				 
 				cell2.setCellValue(customer.getSex() );
 			}else{
 				cell2.setCellValue("");
 			}
 			if(customer.getCompany()!=null){
 				 
 				cell3.setCellValue(customer.getCompany() );
 			}else{
 				cell3.setCellValue("");
 			}
 			if(customer.getAddress()!=null){
 				 
 				cell4.setCellValue(customer.getAddress() );
 			}else{
 				cell4.setCellValue("");
 			}
 			if(customer.getEmail()!=null){
 				
 				cell5.setCellValue(customer.getEmail() );
 			}else{
 				cell5.setCellValue("");
 			}
 			if(customer.getBirthday()!=null){
 				SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
 				cell6.setCellValue(sdf.format(customer.getBirthday()));
 			}else{
 				cell6.setCellValue("");
 			}
  			/*dataRow.createCell(0).setCellValue(customer.getUsername()+"");
  			dataRow.createCell(1).setCellValue(customer.getType()+"");
  			dataRow.createCell(2).setCellValue(customer.getBirthday()+"");
  			dataRow.createCell(3).setCellValue(customer.getSex());
  			dataRow.createCell(4).setCellValue(customer.getCompany()+"");
  			dataRow.createCell(5).setCellValue(customer.getAddress()+"");
  			dataRow.createCell(6).setCellValue(customer.getEmail()+"");*/
 		}
  		HttpServletResponse response = ServletActionContext.getResponse();
 		//文件下载：一个流 两个头（1、文件MIME类型  2、文件打开方式（浏览器内部打开，作为附件下载））
 		String filename="客户信息.xls";
 		String agent = ServletActionContext.getRequest().getHeader("User-Agent");
 		//对文件名进行编码
 		filename = FileUtils.encodeDownloadFilename(filename, agent);
 		response.setHeader("content-type", "application/vnd.ms-excel");
 		response.setHeader("content-disposition", "attachment;filename="+filename);
 		OutputStream stream = response.getOutputStream();
 		workbook.write(stream);
     	return NONE;
    }
}
